package com.windfone.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.OrderUtil;
import com.windfone.common.PageUtil;
import com.windfone.model.Goodsqryorder;
import com.windfone.model.ReceiveMsg;
import com.windfone.service.CommonService;
import com.windfone.service.OrderService;

public class GoodsqryorderAction extends ActionSupport {

	/**	 */
	private static final long serialVersionUID = 1L;
	private CommonService commonService;
	private OrderService orderService;
	private ReceiveMsg receiveMsg;
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	private String name;
	private String orderseq;
	private String sendOrderSeq;
	private String receiveAddress;
	private int keygroup;
	private String operateFlag = "";	
	private String beginDate;
	private String endDate;
	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private int beginRecord;
	public int getBeginRecord() {
		return beginRecord;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public CommonService getCommonService() {
		return commonService;
	}

	public String getOperateFlag() {
		return operateFlag;
	}

	public void setOperateFlag(String operateFlag) {
		this.operateFlag = operateFlag;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOrderseq() {
		return orderseq;
	}

	public void setOrderseq(String orderseq) {
		this.orderseq = orderseq;
	}

	public String getSendOrderSeq() {
		return sendOrderSeq;
	}

	public void setSendOrderSeq(String sendOrderSeq) {
		this.sendOrderSeq = sendOrderSeq;
	}

	public String getReceiveAddress() {
		return receiveAddress;
	}

	public void setReceiveAddress(String receiveAddress) {
		this.receiveAddress = receiveAddress;
	}

	public int getKeygroup() {
		return keygroup;
	}

	public void setKeygroup(int keygroup) {
		this.keygroup = keygroup;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public String beforeAddGoodsqryorder() throws Exception {
//		receiveMsg = orderService.getReceiveMsgByOrderId(orderSeq);
		return SUCCESS;
	}
	
	public ReceiveMsg getReceiveMsg() {
		return receiveMsg;
	}

	public void setReceiveMsg(ReceiveMsg receiveMsg) {
		this.receiveMsg = receiveMsg;
	}

	public String addGoodsqryorder() throws Exception {
		Goodsqryorder bean = new Goodsqryorder();
		 keygroup =OrderUtil.getDateInt();;
		bean.setKeygroup(keygroup);
		bean.setName(name);
		bean.setOrderseq(orderseq);	
		bean.setSendOrderSeq(sendOrderSeq);
		commonService.inserGoodsqryorder( bean );
		return SUCCESS;
	}
	
	private Date getDateFromStr(String dateStr ){
		
		String year = dateStr.substring(0,4);
		String month = dateStr.substring(4,6);
		String day = dateStr.substring(6,8);
//		System.out.println( year);
//		System.out.println( month);
//		System.out.println( day);
		String dateStr2 = year + "-"+month + "-"+ day;
		String formatStr = "yyyy-MM-dd";
		SimpleDateFormat s=new SimpleDateFormat(formatStr);
		Date date = null;
	 	try {
			date= s.parse( dateStr2 );
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
		
	}
	public String addGoodsqryorderByHandle() throws Exception {
		Goodsqryorder bean = new Goodsqryorder();
		bean.setKeygroup(keygroup);
		bean.setName(name);
		bean.setOrderseq(orderseq);	
		bean.setSendOrderSeq(sendOrderSeq);
		bean.setReceiveAddress(receiveAddress);
		if ( ("" + keygroup ).length()== 8 ){
			bean.setCreateTime(  getDateFromStr(""+keygroup ) );
			commonService.inserGoodsqryorderByHandle( bean );
		}else {
			operateFlag = "订单产生日期组数据位数不对" ;
		}
		return SUCCESS;
	}
	
	public static void main(String args[]){
		int keystr = 20100123;
		String dateStr = "" + keystr;
		String year = dateStr.substring(0,4);
		String month = dateStr.substring(4,6);
		String day = dateStr.substring(6,8);
		System.out.println( year);
		System.out.println( month);
		System.out.println( day);
		String dateStr2 = year + "-"+month + "-"+ day;
		String formatStr = "yyyy-MM-dd";
		SimpleDateFormat s=new SimpleDateFormat(formatStr);
	 	try {
			Date date= s.parse( dateStr2 );
			System.out.println(date.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}
 
	private List<Goodsqryorder> goodsqryorders;
	public List<Goodsqryorder> getGoodsqryorders() {
		return goodsqryorders;
	}

	public void setGoodsqryorders(List<Goodsqryorder> goodsqryorders) {
		this.goodsqryorders = goodsqryorders;
	}

	public int goodsqryordersId;
	public int getGoodsqryordersId() {
		return goodsqryordersId;
	}

	public void setGoodsqryordersId(int goodsqryordersId) {
		this.goodsqryordersId = goodsqryordersId;
	}

	public String delGoodsqryorders() throws Exception {
		Goodsqryorder bean = new Goodsqryorder();
		bean.setId(goodsqryordersId);
		commonService.deleGoodsqryorder(bean );
		return SUCCESS;
	}
	public String listGoodsqryorders() throws Exception {
		String whereCondition = "";
	 
 		if ( beginDate!=null && !beginDate.equals("") ){
			whereCondition = whereCondition + "  AND  createTime>='"+beginDate +"' ";
		}
		if ( endDate!=null && !endDate.equals("") ){
			whereCondition = whereCondition + "  AND  createTime<='"+ endDate  +"'  ";
		}
		/*
		List<Goodsqryorder> allGroups= commonService.getAllKewgroupsByWhere( whereCondition );*/
//		int keygroup =OrderUtil.getDateInt();;
//		bean.setKeygroup(keygroup);
//		bean.setName(name);
//		bean.setOrderseq(orderseq);	
//		bean.setSendOrderSeq(sendOrderSeq);
//		commonService( bean );
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = commonService.getAllGoodsqryorderRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		goodsqryorders = commonService.getAllGoodsqryorder(beginRecord,whereCondition);
		return SUCCESS;
	}

}
